package art.client.activites;

import java.util.Map;

import art.client.ClientFactory;
import art.client.ClientUtils;
import art.client.places.ArtsListPlace;
import art.client.views.UserInfoView;
import art.shared.entities.LoginInfo;
import art.shared.entities.UserShared;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class UserInfoActivity extends AbstractActivity {

	private ClientFactory clientFactory = null;
	private LoginInfo loginInfo = null;

	public UserInfoActivity(ClientFactory clientFactory, LoginInfo loginInfo) {
		this.clientFactory = clientFactory;
		this.loginInfo = loginInfo;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		initView(containerWidget);
	}

	private void initView(final AcceptsOneWidget containerWidget) {
		if (loginInfo == null) {
			ClientUtils.getUserService().getLoginInfo(
					new AsyncCallback<LoginInfo>() {

						@Override
						public void onSuccess(LoginInfo loginInfo) {
							executeInit(containerWidget);
						}

						@Override
						public void onFailure(Throwable arg0) {

						}
					});
		} else {
			executeInit(containerWidget);
		}
	}

	private void executeInit(final AcceptsOneWidget containerWidget) {
		UserInfoView userInfoView = clientFactory.getUserInfoView(loginInfo);
		containerWidget.setWidget(userInfoView.asWidget());
		userInfoView.insertData(loginInfo.getUser());
		initButtonListeners(userInfoView);
	}

	private void initButtonListeners(final UserInfoView userInfoView) {
		userInfoView.getSaveButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Map<String, Widget> widgets = userInfoView.getWidgets();
				final UserShared user = userInfoView.getUser();
				user.setName(((TextBox) widgets.get("name")).getText());
				user.setNickname(((TextBox) widgets.get("nickname")).getText());
				user.setDateOfBirth(((DateBox) widgets.get("dateOfBirth"))
						.getValue());
				int genderIndex = ((ListBox) widgets.get("gender"))
						.getSelectedIndex();
				switch (genderIndex) {
				case 1:
					user.setGender(UserShared.Gender.FEMALE);
					break;
				case 2:
					user.setGender(UserShared.Gender.MALE);
					break;
				default:
					user.setGender(null);
					break;
				}
				ClientUtils.getUserService().updateUser(user,
						new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable arg0) {
								Window.alert("Error: Couldn't save changes!");
							}

							@Override
							public void onSuccess(Boolean arg0) {
								final DecoratedPopupPanel popup = new DecoratedPopupPanel();
								VerticalPanel panel = new VerticalPanel();
								panel.add(new Label(
										"Changes saved successfully."));
								loginInfo.setUser(user);
								Button okButton = new Button("OK");
								okButton.addClickHandler(new ClickHandler() {
									@Override
									public void onClick(ClickEvent event) {
										popup.hide();
										clientFactory.getPlaceController()
												.goTo(new ArtsListPlace("arts",
														loginInfo));
									}
								});
								panel.add(okButton);
								popup.add(panel);
								popup.show();
							}
						});
			}
		});
		userInfoView.getCancelButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				clientFactory.getPlaceController().goTo(
						new ArtsListPlace("arts", loginInfo));
			}
		});

	}

}
